import GameTools from "GameTools";
import GameConfig from "./GameConfig";

var GameInit = {
    gameInit: function() {
        // 获取配置
        this._getConfig();
        // 转发设置
        this._shareMenu();
    },

    _shareMenu: function() {
        // 显示微信转发
        wx.showShareMenu();
        // 设置转发内容
        cc.loader.loadRes("shareIcon",function(err,data){
            wx.onShareAppMessage(function () {
                return {
                    title: GameConfig.SHARE_TITLE,
                    imageUrl: data.url
                }
            })
        });
    },
    _getConfig: function() {
        let _self = this;
        GameTools.ajax('/parkour/game/getConfig', {}, function(data){
            if (data.status == 1) {
                data = data.data;
                if (data) {
                    if (data['shareTitle']) {
                        GameConfig.SHARE_TITLE = data['shareTitle'];
                    }
                    if (data['resultShareTitle']) {
                        GameConfig.RESULT_SHARE_TITLE = data['resultShareTitle'];
                    }
                    if (data['propsPlayerMoveSpeedRange']) {
                        GameConfig.PROPS_PLAYER_MOVE_SPEED_RANGE = _self._dealIntArray(data['propsPlayerMoveSpeedRange']);
                    }
                    if (data['speedLevelScore']) {
                        GameConfig.SPEED_LEVEL_SCORE = _self._dealIntArray(data['speedLevelScore']);
                    }
                    if (data['propsPlayerMoveSpeed']) {
                        let speed = _self._dealInt(data['propsPlayerMoveSpeed']);
                        if (speed) {
                            GameConfig.PROPS_PLAYER_MOVE_SPEED = speed;
                        }
                    }
                    if (data['difficultyLevelScore']) {
                        GameConfig.DIFFICULTY_LEVEL_SCORE = _self._dealIntArray(data['difficultyLevelScore']);
                    }
                    if (data['levelOfDifficulty']) {
                        let levelOfDifficulty = _self._dealInt(data['levelOfDifficulty']);
                        if (levelOfDifficulty) {
                            GameConfig.LEVEL_OF_DIFFICULTY = levelOfDifficulty;
                        }
                    }
                    if (data['gainScoreSpeed']) {
                        let gainScoreSpeed = _self._dealInt(data['gainScoreSpeed']);
                        if (gainScoreSpeed) {
                            GameConfig.GAIN_SCORE_SPEED = gainScoreSpeed;
                        }
                    }
                    if (data['barrierUnitDistance']) {
                        let barrierUnitDistance = _self._dealInt(data['barrierUnitDistance']);
                        if (barrierUnitDistance) {
                            GameConfig.BARRIER_UNIT_DISTANCE = barrierUnitDistance;
                        }
                    }
                    if (data['initProbabilityOfBarrier']) {
                        let initProbabilityOfBarrier = _self._dealInt(data['initProbabilityOfBarrier']);
                        if (initProbabilityOfBarrier) {
                            GameConfig.INIT_PROBABILITY_OF_BARRIER = initProbabilityOfBarrier;
                        }
                    }
                    if (data['unitProbabilityOfBarrier']) {
                        let unitProbabilityOfBarrier = _self._dealInt(data['unitProbabilityOfBarrier']);
                        if (unitProbabilityOfBarrier) {
                            GameConfig.UNIT_PROBABILITY_OF_BARRIER = unitProbabilityOfBarrier;
                        }
                    }
                    if (data['defenseRate']) {
                        let defenseRate = _self._dealInt(data['defenseRate']);
                        if (defenseRate) {
                            GameConfig.DEFENSE_RATE = defenseRate;
                        }
                    }
                    if (data['unitDistance']) {
                        let unitDistance = _self._dealInt(data['unitDistance']);
                        if (unitDistance) {
                            GameConfig.DEFENSE_UNIT_DISTANCE = unitDistance;
                        }
                    }
                    if (data['propsRate']) {
                        let propsRate = _self._dealInt(data['propsRate']);
                        if (propsRate) {
                            GameConfig.SCORE_PROPS_RATE = propsRate;
                        }
                    }
                    if (data['scorePropsUnitDistance']) {
                        let scorePropsUnitDistance = _self._dealInt(data['scorePropsUnitDistance']);
                        if (scorePropsUnitDistance) {
                            GameConfig.SCORE_PROPS_UNIT_DISTANCE = scorePropsUnitDistance;
                        }
                    }
                    if (data['rotationUnitDistance']) {
                        let rotationUnitDistance = _self._dealInt(data['rotationUnitDistance']);
                        if (rotationUnitDistance) {
                            GameConfig.ROTATION_UNIT_DISTANCE = rotationUnitDistance;
                        }
                    }
                    if (data['navigatePrograms']) {
                        try {
                            GameConfig.NAVIGATE_PROGRAMS = JSON.parse(data['navigatePrograms']);
                        } catch(err) {

                        }
                    }
                }
            }
        }, function(data) {});
    },
    _dealIntArray: function(item) {
        let arr = item.split(",");
        let res = [];
        arr.map(function(value){
            try {
                res.push(parseInt(value.trim()));
            } catch(err) {
                continue;
            }
        });
        return res;
    },
    _dealInt: function(item) {
        try {
            return parseInt(item);
        } catch(err) {
            return null;
        }
    }
};

export default GameInit;
